package org.springframework.prospring.ticket.dao.hibernate;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.prospring.ticket.dao.SurfaceTypeDAO;
import org.springframework.prospring.ticket.domain.SurfaceType;
import org.springframework.prospring.ticket.exception.BusinessException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.lang.StringUtils;

import java.util.Collection;

/**
 * This class interacts with Spring and Hibernate to save and
 * retrieve User objects.
 *
 * @author Alexis Quiros
 */
public class SurfaceTypeDAOHibernate extends HibernateDaoSupport implements SurfaceTypeDAO {
    private static Log logger = LogFactory.getLog(SurfaceTypeDAOHibernate.class);

    public SurfaceTypeDAOHibernate() {
        super();
        if (logger.isDebugEnabled()) {
            logger.debug("in SurfaceTypeDAOHibernate Constructor");
        }
    }

    public SurfaceType findSurfaceType(Long surfaceTypeId){
        if (SurfaceTypeDAOHibernate.logger.isDebugEnabled()) {
            SurfaceTypeDAOHibernate.logger.debug("finding SurfaceType id: " + surfaceTypeId);
        }

        SurfaceType surfaceType = (SurfaceType)getHibernateTemplate().load(SurfaceType.class, surfaceTypeId);

        return surfaceType;        
    }

    public Collection<SurfaceType> findAllSurfaceTypes(){
        if (logger.isDebugEnabled()) {
            logger.debug("finding all SurfaceType ");
        }

        Collection surfaceTypes = getHibernateTemplate().find("from SurfaceType");

        return surfaceTypes;
    }
    public void saveSurfaceType(SurfaceType surfaceType) throws Exception {
        if (logger.isDebugEnabled()) {
            logger.debug("Saving SurfaceType: " + surfaceType);
        }

        if (StringUtils.isEmpty(surfaceType.getName())) {
            String message = "The name is required, name="+surfaceType.getName();
            logger.info(message);
            throw new BusinessException(message);
        }

        if (StringUtils.isEmpty(surfaceType.getBusinessPK())) {
            String message = "The businessPK is required, businessPK="+surfaceType.getBusinessPK();
            logger.info(message);
            throw new BusinessException(message);
        }

        getHibernateTemplate().saveOrUpdate(surfaceType);

        if (logger.isDebugEnabled()) {
            logger.debug("surfaceTypeId set to: " + surfaceType.getId());
        }
    }

    public void removeSurfaceType(Long surfaceTypeId) {
        if (logger.isDebugEnabled()) {
            logger.debug("removing SurfaceType id: " + surfaceTypeId);
        }

        Object surfaceType = getHibernateTemplate().load(SurfaceType.class, surfaceTypeId);
        getHibernateTemplate().delete(surfaceType);
    }

    public SurfaceType findByBusinessPK(String surfaceTypeBusinessPK) {
        if (logger.isDebugEnabled()) {
            logger.debug("finding SurfaceType by businessPK="+surfaceTypeBusinessPK);
        }

        SurfaceType surfaceType = null;
        String queryString = "from SurfaceType s where (s.businessPK = :businessPK)";
        SurfaceType surfaceTypeParameter = new SurfaceType();
        surfaceTypeParameter.setBusinessPK(surfaceTypeBusinessPK);
        Collection<SurfaceType> surfaceTypes = getHibernateTemplate().findByValueBean(queryString, surfaceTypeParameter);
        //Collection<Region> sports = getHibernateTemplate().findByValueBean.find("from Region r where r.businessPK="+sportBusinessPK);

        if ((surfaceTypes != null) && (!surfaceTypes.isEmpty())){
            surfaceType = surfaceTypes.iterator().next();
        }

        return surfaceType;
    }


}
